#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

def Search_CaseSensitive_Return(regex, line):
    import re
    pattern = re.compile(regex)
    m = re.search(pattern, line)
    if m:
        return m.group(1)
    else:
        return m


def Search_CaseIgnore_Return(regex, line):
    import re
    pattern = re.compile(regex, re.I)
    m = re.search(pattern, line)
    if m:
        return m.group(1)
    else:
        return m


def Search_Symbol(regex, line):
    import re
    pattern = re.compile(regex, re.I)
    m = re.search(pattern, line)
    if m:
        return m.group(2)
    else:
        return line


if __name__ == "__main__":
    from pymongo import MongoClient
    import re

    import sys

    default_encoding = 'utf-8'
    if sys.getdefaultencoding() != default_encoding:
        reload(sys)
        sys.setdefaultencoding(default_encoding)

    DB_NAME = "organization"
    COLLECTION_NAME = "organization"

    connection = MongoClient('localhost', 27017)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    regex_label = "<.*?>"
    regex_sup = "<sup>.*?</sup>"
    regex_blank = " +"

    regex_arr = []
    type_arr = []
    infile_org_type = open("org_type", "r")
    for line in infile_org_type:
        line = line.rstrip()
        splitline = line.split("\t")
        type_arr.append(splitline[0])
        regex_arr.append("[,;]? *([^,;]*" + splitline[1] + "[^,\.;]*)[,\.;]?")
    infile_org_type.close()

    infile_symbol = open("symbol", "r")
    regex_s = infile_symbol.readline().rstrip()
    infile_symbol.close()
    regex_symbol = "^" + regex_s + "(.*?)" + regex_s + "$"

    results = collection.find({"country": "China"})
    for result in results:
        org = result["org"]
        id = result["id"]
        org2 = re.sub(regex_sup, "", org, 0, re.I)
        org2 = re.sub(regex_label, "", org2, 0, re.I)
        org2 = re.sub(regex_blank, " ", org2, 0, re.I)
        line_found = None
        for i in xrange(0, len(regex_arr)):
            found = Search_CaseIgnore_Return(regex_arr[i], org2)
            if found != None:
                line_found = found
                line_found = Search_Symbol(regex_symbol, line_found)
                collection.update({"id": id}, {"$set": {"uni_org": line_found, "uni_org_type": type_arr[i]}})
                break
        if line_found != None:
            print line_found

    connection.close()
